//
//  UIColor+Utils.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/2/8.
//

import Foundation
import Foundation
import UIKit

extension UIColor {
    
    ///随机色
    static var random: UIColor {
        let red = CGFloat.random(in: 0...255)
        let green = CGFloat.random(in: 0...255)
        let blue = CGFloat.random(in: 0...255)
        return UIColor(red: red, green: green, blue: blue, alpha: 1)
    }
    
    static var themeBlueColor: UIColor {
        return UIColor(hex: "#E7F3FF") ?? UIColor(red: 54.0 / 255.0, green: 134.0 / 255.0, blue: 246.0 / 255.0, alpha: 1.0)
    }
    
    static var themeColor: UIColor {
        return UIColor(hex: "#6E42E5") ?? UIColor(red: 54.0 / 255.0, green: 134.0 / 255.0, blue: 246.0 / 255.0, alpha: 1.0)
    }
    
    static var appTint: UIColor {
        return UIColor(hex: "#0055FF") ?? UIColor(red: 54.0 / 255.0, green: 134.0 / 255.0, blue: 246.0 / 255.0, alpha: 1.0)
    }
    static var appSeparator: UIColor {
        return UIColor(red: 233.0 / 255.0, green: 237.0 / 255.0, blue: 246.0 / 255.0, alpha: 1.0)
    }
    static var appBackGround: UIColor {
        return UIColor(hex: "#F4F4F4") ?? .black
    }
    static var appWhiteGround: UIColor {
        return UIColor(hex: "#F5F6FA") ?? .groupTableViewBackground
    }
    
    static var lineColor: UIColor {
        return UIColor(hex: "#CCCCCC") ?? .black
    }
    
    static var app3Color: UIColor {
        return UIColor(hex: "#333333") ?? .black
    }
    
    static var app6Color: UIColor {
        return UIColor(hex: "#666666") ?? .black
    }
    
    static var app9Color: UIColor {
        return UIColor(hex: "#999999") ?? .black
    }
    /// 常用字体黑色
    static var appTextColor: UIColor {
        return UIColor(hex: "#28334A") ?? .black
    }

    // MARK: - Initialization
    convenience init?(hex: String) {
        var hexSanitized = hex.trimmingCharacters(in: .whitespacesAndNewlines)
        hexSanitized = hexSanitized.replacingOccurrences(of: "#", with: "")
        var rgb: UInt64 = 0
        var r: CGFloat = 0.0
        var g: CGFloat = 0.0
        var b: CGFloat = 0.0
        var a: CGFloat = 1.0
        let length = hexSanitized.count
        guard Scanner(string: hexSanitized).scanHexInt64(&rgb) else { return nil }
        if length == 6 {
            r = CGFloat((rgb & 0xFF0000) >> 16) / 255.0
            g = CGFloat((rgb & 0x00FF00) >> 8) / 255.0
            b = CGFloat(rgb & 0x0000FF) / 255.0

        } else if length == 8 {
            r = CGFloat((rgb & 0xFF000000) >> 24) / 255.0
            g = CGFloat((rgb & 0x00FF0000) >> 16) / 255.0
            b = CGFloat((rgb & 0x0000FF00) >> 8) / 255.0
            a = CGFloat(rgb & 0x000000FF) / 255.0

        } else {
            return nil
        }
        self.init(red: r, green: g, blue: b, alpha: a)
    }
    
    /// 使用 RGB 的 UInt8 数值,以及 alpha 0~1 初始化 UIColor
        /// - Parameters:
        ///   - red: 红色值
        ///   - green: 绿色值
        ///   - blue: 蓝色值
        ///   - alpha: 透明度 0~1
    convenience init(redColor: UInt8, greenColor: UInt8, blueColor: UInt8, alpha: CGFloat) {

        self.init(red: CGFloat(redColor) / 255.0,
                      green: CGFloat(greenColor) / 255.0,
                      blue: CGFloat(blueColor) / 255.0,
                      alpha: alpha)
    }
    
}

public extension Array where Element: UIColor {
  ///通过颜色数组生成渐变色
  func gradient(_ transform: ((_ gradient: inout CAGradientLayer) -> CAGradientLayer)? = nil) -> CAGradientLayer {
    var gradient = CAGradientLayer()
    gradient.colors = self.map { $0.cgColor }
    
    if let transform = transform {
      gradient = transform(&gradient)
    }
    
    return gradient
  }
}

